package com.mall.config;

import cn.hutool.core.collection.CollUtil;
import com.mall.common.utils.JwtUtils;
import com.mall.interceptor.LoginInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(AuthProperties.class)
public class MvcConfig implements WebMvcConfigurer {

    private final AuthProperties authProperties;

    private final JwtUtils jwtUtils;

    /**
     * 注册拦截器
     * 123test
     * @param interceptorRegistry
     */
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry){
        // 1.添加拦截器
        LoginInterceptor loginInterceptor=new LoginInterceptor(jwtUtils);
        InterceptorRegistration registration = interceptorRegistry.addInterceptor(loginInterceptor);
        // 2.配置拦截路径
        List<String> includePaths = authProperties.getIncludePaths();
        if (CollUtil.isNotEmpty(includePaths)) {
            registration.addPathPatterns(includePaths);
        }
        List<String> excludePaths = authProperties.getExcludePaths();
        if (CollUtil.isNotEmpty(excludePaths)) {
            registration.excludePathPatterns(excludePaths);
        }
        registration.excludePathPatterns(
                "/search/**",
                "/users/login",
                "/users/register",
                "/users/verification/code",
                "/today-recommend",
                "/guess-you-like",
                "/items/**"
        );
    }

}
